<template>
  <div ref="container">
    <a-modal
      :title="title"
      :width="800"
      :ok=false
      :visible="visible"
      :confirmLoading="confirmLoading"
      :getContainer="() => $refs.container"
      :maskStyle="{'top':'93px','left':'154px'}"
      :wrapClassName="wrapClassNameInfo()"
      :mask="isDesktop()"
      :maskClosable="false"
      :okButtonProps="{ props: {disabled: disableSubmit} }"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭"
      style="top:50px;height: 80%;">
      <a-spin :spinning="confirmLoading">
        <a-form :form="form" id="quoteTemplateItemForm">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="数量">
            <a-input placeholder="请输入名称" v-decorator="['count', validatorRules.name ]"/>
          </a-form-item>
        </a-form>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import {  queryQuoteCategoryTreeList } from '@/api/api'
  import {mixinDevice} from '@/utils/mixin'
  import pick from 'lodash.pick'
  import ATextarea from 'ant-design-vue/es/input/TextArea'
  export default {
    name: "QuoteProjectItemModal",
    mixins: [mixinDevice],
    components: { ATextarea },
    data () {
      return {
        mode : '',
        id:'',
        quoteCategoryId:'',
        title:"操作",
        visible: false,
        disableSubmit:false,
        model: {},
        menuhidden:false,
        menuusing:true,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          name: {
            rules: [
              { required: true, message: '请输入名称!'},
              { validator: this.validateName}
            ]
          },
        },
        url: {
          edit: "/quote/project/item/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add (quoteCategoryId) {
        console.log('quoteCategoryId',quoteCategoryId)
        this.quoteCategoryId = quoteCategoryId
        this.mode = 'add';
        this.form.resetFields();
        this.model = Object.assign({}, {});
        this.visible = true;
      },
      edit (record) {
        console.log('record',record);
        this.mode = 'edit';
        this.id = record.id;
        this.form.resetFields();
        this.model = Object.assign({}, {});
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(record, 'id', 'count'))
        });
      },
      close () {
        this.$emit('close');
        this.disableSubmit = false;
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            if ( that.mode === 'edit' ){
              formData.id = that.id
            }
            httpAction(this.url.edit,formData,"post").then((res)=>{
              if(res.code == 200){
                that.$message.success(res.data.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.data.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>

<style scoped>
  @import '~@assets/less/common.less';
</style>